package com.cherish.meeting.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cherish.meeting.api.service.WxService;
import com.cherish.meeting.common.http.HttpComponent;
import com.cherish.meeting.common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author: wuhong-016
 * @time: 2021/3/11 18:05
 * @version: v1.0.0
 * @description:TODO
 */
@Slf4j
@Service
public class WxServiceImpl implements WxService {

    private String domain = "https://api.weixin.qq.com";

    @Autowired
    private HttpComponent http;


    private final String appid = "wx35c629d3f32531fb";
    private final String secret = "31e735ea5f400b6d94998826b463e45f";

    @Override
    public String getOpenId(String code) {
        StringBuffer sb = new StringBuffer(domain)
                .append("/sns/jscode2session?appid=")
                .append(appid)
                .append("&secret=")
                .append(secret)
                .append("&js_code=")
                .append(code)
                .append("&grant_type=authorization_code");
        String result = http.doGet(sb.toString());
        if (StringUtil.isNotEmpty(result)) {
            JSONObject json = JSON.parseObject(result);
            return json.getString("openid");
        }
        return null;
    }

}
